package com.orangelife.mobile.individual.activity;

import java.util.HashMap;
import java.util.Map;

import com.common.jump.CommonJumper;
import com.common.sp.UserSp;
import com.curry.android.http.HttpUtil;
import com.curry.android.util.DialogHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.util.StringUtil;
import com.orangelife.mobile.util.ToastHelper;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.TextView;

/**
 * 建议反馈界面
 *
 * @author songhuangzhong
 */
public class MenuSuggestionActivity extends OrangeLifeBaseActivity {
    /** 建议的内容 */
    private TextView etFeedbackContent;
    /** 页面的标题 */
    private TextView tv_title, tv_title_score;
    /** 提交建议反馈的按钮 */
    private Button btnSubmitFeedback;
    /** 进度条 */
    private Dialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu_suggestion);
        initView();
        setText();
        setListener();
    }

    /** 初始化组件 */
    private void initView() {
        etFeedbackContent = (TextView) findViewById(R.id.etFeedbackContent);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title_score = (TextView) findViewById(R.id.tv_title_score);
        btnSubmitFeedback = (Button) findViewById(R.id.btnSubmitFeedback);
    }

    /** 配置监听器 */
    private void setListener() {
        btnSubmitFeedback.setOnClickListener(clickListener);
    }

    /** 点击隐藏输入法 */
    public void hideInput(View v) {
        InputMethodManager imm = (InputMethodManager) this
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        // edit.setCursorVisible(false);//失去光标
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }

    /** 为界面上的控件赋值 */
    private void setText() {
        tv_title.setText(getResources().getString(R.string.advice_fankui));
        tv_title_score.setText(UserSp.getPoint());
    }

    /** 提交建议的按钮 */
    View.OnClickListener clickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btnSubmitFeedback:
                    String str = etFeedbackContent.getText().toString();
                    boolean isOk = StringUtil.isBlank(str);
                    if (!isOk) {
                        if (StringUtil.isBlank(UserSp.getLoginID())) {
                            CommonJumper.jumpToLogin(MenuSuggestionActivity.this);
                        } else {
                            sendSuggestion();
                            dialog = DialogHelper.getInstance().createLoadingDialog(
                                    MenuSuggestionActivity.this, Constant.LOADING);
                            dialog.show();
                        }
                    } else {
                        ToastHelper.getInstance().displayToastShort(
                                getResources().getString(R.string.feedback_hint));
                    }
                    break;

                default:
                    break;
            }

        }
    };

    /** 提交建议反馈请求 */
    private void sendSuggestion() {
        String memberID = UserSp.getLoginID();
        String content = etFeedbackContent.getText().toString();
        Map<String, Object> inforMap = new HashMap<String, Object>();
        inforMap.put("memberID", memberID);
        inforMap.put("content", content);
        Map<String, Object> rootMap = new HashMap<String, Object>();
        rootMap.put("map", inforMap);
        rootMap.put("url", Constant.URL_SUGGESTION);
        rootMap.put("wat", 1);
        new JSONRequest(JSONRequest.POST, rootMap, handler,
                HttpUtil.ACTION_JSON);

    }

    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    ToastHelper.getInstance()._toast(
                            getResources().getString(
                                    R.string.feedback_submit_success));
                    finish();
                    if (dialog != null) {
                        dialog.dismiss();
                    }
                    break;
                case Constant.WHAT_ERROR_HTTP:
                    ToastHelper.getInstance()._toast(String.valueOf(msg.obj));
                    break;
                case 1011:
                    isLogin(dialog);
                    break;
                default:
                    break;
            }
        }

    };

}
